<template>
	<div class="widgets bg-white">
		<div class="searchDiv left">
            <el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
            <el-button type="primary" @click="search">查询</el-button>
        </div>
        <div class="btn-group right">
        	<el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" v-if="isDisabled" disabled>新增</el-button>
        	<el-button type="primary" v-else @click="add">新增</el-button>
            <el-button type="danger" v-if="multipleSelection.length != 1" disabled>删除</el-button>
        	<el-button type="danger" v-else @click="del">删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
        	<el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
        	<el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
        	<el-button type="warning" @click="openSubjectGroup">科目组</el-button>
        	<el-button-group class="btn-other">
                <el-button v-if="multipleSelection.length <= 0" disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导出" v-else @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>                 
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
            isDisabled: true,       //是否禁用
            node: this.$store.state.accountSubjectStore.node,
            multipleSelection: this.$store.state.accountSubjectStore.multipleSelection,
            selectRowId: this.$store.state.accountSubjectStore.selectRowId
		}
	},
    watch: {
        node(newVal) {
            newVal[0]['code'].split('.')[0] === '101' ? this.isDisabled = true : this.isDisabled = false;
        },
        searchText(newVal) {
            if(!newVal) {
                this.$store.dispatch('accountSubjectStore/getTreeData');
            }
        }
    },
	methods: {
		search() {        //查询
            if(this.searchText) {
                let searchResult = this.$store.state.accountSubjectStore.searchResult,
                    newArr = [],
                    searchText = this.searchText,
                    checkName = ['code', 'name', 'toPinYinName', 'typeName', 'toPinYinTypeName', 'foreignCurrencyCheckName', 'toPinYinForeignCurrencyCheckName', 'balanceDirectionName', 'toPinYinBalanceDirectionName'];   //要搜索的键名
                if(searchResult.length > 0) {
                    searchResult.forEach(item => {
                        checkName.forEach(key => {
                            if(item[key] && item[key].indexOf(searchText) > -1) {
                                newArr.push(item);
                            }
                        });
                    });
                }
                if(newArr.length > 0) {
                    this.$store.commit('accountSubjectStore/updateTableData', newArr);
                } else {
                    this.$store.commit('accountSubjectStore/updateTableData', []);
                }
            }
		},
        refresh() {     //刷新
            this.$store.dispatch('accountSubjectStore/getTreeData');
        },
        add() {     //新增
            this.$store.commit('accountSubjectStore/updateRowId');      //清空rowId
            this.$store.commit('accountSubjectStore/clearBetweenObject');       //清空往来对象数据
            this.$util.openLayerIframe({title: '会计科目-新增', zIndex: 13, width: '600px', height: '480px', url: this.$domain + '/#/basicData/accountSubject/tab'});
        },
        del() {         //删除会计科目
            this.$confirm('确定删除选中数据？', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.get('subjectGroup/deleteGroupById?id=' + this.selectRowId[0]).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({type: 'success', message: '删除成功', duration: 1500});
                        setTimeout(() => {
                            this.$store.dispatch('accountSubjectStore/getTreeData');        //刷新数据
                        }, 1000);
                    } else {
                        this.$message.error(res.data.msg || '删除失败！');
                    }
                });
            }).catch(() => {
                this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
        },
        disabedBtn(status, msg) {       //禁用、启用
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['locks'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('subjectGroup/enable?state=' + status, JSON.stringify(this.selectRowId), {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
                    setTimeout(() => {
                        let node = this.$store.state.accountSubjectStore.node;
                        //禁用、启用只需要刷新表格数据即可，不需要刷新树形结构数据
                        this.$store.dispatch('accountSubjectStore/getSubjectTableDataById', node[0]['id']);
                        this.$store.commit('accountSubjectStore/multipleSelection', []);        //清空上一次选中的数据
                    }, 1000);
                } else {
                    this.$message.error(res.data.msg);
                }
            });
        },
        exportExcel() {     //导出
            var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('subjectGroup/export',{params: {list: idSting}}).then(res => {
                this.$message({message: '导出成功', type: 'success', duration: 1500});
                window.location.href = this.$url + 'subjectGroup/export?list=' + idSting;          //导出时触发
            }).catch(err => {
                this.$message.error(err);
            });
        },
        openSubjectGroup() {        //打开科目组
            this.$util.openLayerIframe({title: '会计科目类别管理', zIndex: 13, width: '600px', height: '400px', url: this.$domain + '/#/basicData/accountSubject/subjectGroup'});
        }
	}
}
</script>